home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 25 / AACD 25.iso / AACD / Magazine / Online / QMail / source / maildir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-04-15  |  2.3 KB  |  109 lines

  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include "prioq.h"
  4. #include "env.h"
  5. #include "stralloc.h"
  6. #include "direntry.h"
  7. #include "datetime.h"
  8. #include "now.h"
  9. #include "str.h"
  10. #include "maildir.h"
  11.  
  12. struct strerr maildir_chdir_err;
  13. struct strerr maildir_scan_err;
  14.  
  15. int maildir_chdir()
  16. {
  17.  char *maildir;
  18.  maildir = env_get("MAILDIR");
  19.  if (!maildir)
  20.    STRERR(-1,maildir_chdir_err,"MAILDIR not set")
  21.  if (chdir(maildir) == -1)
  22.    STRERR_SYS3(-1,maildir_chdir_err,"unable to chdir to ",maildir,": ")
  23.  return 0;
  24. }
  25.  
  26. void maildir_clean(tmpname)
  27. stralloc *tmpname;
  28. {
  29.  DIR *dir;
  30.  direntry *d;
  31.  datetime_sec time;
  32.  struct stat st;
  33.  
  34.  time = now();
  35.  
  36.  dir = opendir("tmp");
  37.  if (!dir) return;
  38.  
  39.  while (d = readdir(dir))
  40.   {
  41.    if (d->d_name[0] == '.') continue;
  42.    if (!stralloc_copys(tmpname,"tmp/")) break;
  43.    if (!stralloc_cats(tmpname,d->d_name)) break;
  44.    if (!stralloc_0(tmpname)) break;
  45.    if (stat(tmpname->s,&st) == 0)
  46.      if (time > st.st_atime + 129600)
  47.        unlink(tmpname->s);
  48.   }
  49.  closedir(dir);
  50. }
  51.  
  52. static int append(pq,filenames,subdir,time)
  53. prioq *pq;
  54. stralloc *filenames;
  55. char *subdir;
  56. datetime_sec time;
  57. {
  58.  DIR *dir;
  59.  direntry *d;
  60.  struct prioq_elt pe;
  61.  unsigned int pos;
  62.  struct stat st;
  63.  
  64.  dir = opendir(subdir);
  65.  if (!dir)
  66.    STRERR_SYS3(-1,maildir_scan_err,"unable to scan $MAILDIR/",subdir,": ")
  67.  
  68.  while (d = readdir(dir))
  69.   {
  70.    if (d->d_name[0] == '.') continue;
  71.    pos = filenames->len;
  72.    if (!stralloc_cats(filenames,subdir)) break;
  73.    if (!stralloc_cats(filenames,"/")) break;
  74.    if (!stralloc_cats(filenames,d->d_name)) break;
  75.    if (!stralloc_0(filenames)) break;
  76.    if (stat(filenames->s + pos,&st) == 0)
  77.      if (st.st_mtime < time) /* don't want to mix up the order */
  78.       {
  79.        pe.dt = st.st_mtime;
  80.        pe.id = pos;
  81.        if (!prioq_insert(pq,&pe)) break;
  82.       }
  83.   }
  84.  
  85.  closedir(dir);
  86.  if (d) STRERR_SYS3(-1,maildir_scan_err,"unable to read $MAILDIR/",subdir,": ")
  87.  return 0;
  88. }
  89.  
  90. int maildir_scan(pq,filenames,flagnew,flagcur)
  91. prioq *pq;
  92. stralloc *filenames;
  93. int flagnew;
  94. int flagcur;
  95. {
  96.  struct prioq_elt pe;
  97.  datetime_sec time;
  98.  int r;
  99.  
  100.  if (!stralloc_copys(filenames,"")) return 0;
  101.  while (prioq_min(pq,&pe)) prioq_delmin(pq);
  102.  
  103.  time = now();
  104.  
  105.  if (flagnew) if (append(pq,filenames,"new",time) == -1) return -1;
  106.  if (flagcur) if (append(pq,filenames,"cur",time) == -1) return -1;
  107.  return 0;
  108. }
  109.